import { userLoginApi, userInfoApi } from '@/api'
import { setToken, getToken, setId, getId, removeToken } from '@/utils/auth'
export default {
  namespaced: true,
  state: {
    token: getToken() || '',
    userId: getId() || null,
    userInfo: {}
  },
  mutations: {
    SET_TOKEN(state, token) {
      state.token = token
      setToken(token)
    },
    REMOVE_TOKEN(state) {
      state.token = ''
      removeToken()
    },
    SET_USERINFO(state, userInfo) {
      state.userInfo = userInfo
    },
    SET_USERID(state, userId) {
      state.userId = userId
      setId(userId)
    }
  },
  actions: {
    async getUserLogin(store, loginForm) {
      const res = await userLoginApi(loginForm)
      store.commit('SET_TOKEN', res.data.token)
      store.commit('SET_USERID', res.data.userId)
      return res
    },
    async getUserInfo(store) {
      const res = await userInfoApi(store.state.userId)
      // console.log(res)
      store.commit('SET_USERINFO', res.data)
      return res.data
    }
  }
}
